package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分类Service
 *   ServiceImpl类：
 *      我们的业务类可以去这个类，相对BaseMapper接口多个几个批量操作方法
 */
@Service
@Transactional
public class CategoryService extends ServiceImpl<CategoryMapper,Category>{
    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> findCategoriesByPId(Long pid) {
        /**
         *  Wrappers类的方法
         *      query(T): 简单条件查询（不含分页，复杂查询）
         *      query(): 用于复杂查询（分页等）
         */

        //1.封装条件到Wrapper中
        Category category = new Category();
        category.setParentId(pid);

        QueryWrapper<Category> wrapper = Wrappers.query(category);

        //2.执行查询，获取结果
        List<Category> categories = categoryMapper.selectList(wrapper);

        //3.处理结果
        if(CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        //4.返回结果
        return categories;
    }

    public List<Category> findCategoriesByIds(List<Long> ids) {
        List<Category> categories = categoryMapper.selectBatchIds(ids);
        if(CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categories;
    }
}
